Imports MIMS.WRS.Business
Imports MIMS.WRS.Entities
Imports MyFramework

Public Class IDSHDReportHeader
    Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents ltrJS As System.Web.UI.WebControls.Literal
    Protected WithEvents ddlConPoint As System.Web.UI.WebControls.DropDownList
    Protected WithEvents ddlIDSSort As System.Web.UI.WebControls.DropDownList
    Protected WithEvents ddlOrderGoods As System.Web.UI.WebControls.DropDownList
    Protected WithEvents txtQty As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtDiscount As System.Web.UI.WebControls.TextBox
    Protected WithEvents chkShowFOB As System.Web.UI.WebControls.CheckBox
    Protected WithEvents btnOK As System.Web.UI.WebControls.Button
    Protected WithEvents ddlTerritory As System.Web.UI.WebControls.DropDownList
    Protected WithEvents ddlTarget As System.Web.UI.WebControls.DropDownList
    Protected WithEvents ddlFOBOrHD As System.Web.UI.WebControls.DropDownList
    Protected WithEvents ibtnGO As System.Web.UI.WebControls.ImageButton
    Protected WithEvents ibtnExportPDF As System.Web.UI.WebControls.ImageButton

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Public Event OKClick(ByVal conPointID As Int32, ByVal IDSSort As Int32, ByVal quantity As Int32, ByVal discount As Decimal, ByVal showFOB As Boolean, ByVal ownerId As Int32, ByVal target As Int32, ByVal territoryId As Int32)
    Public Event ExportPDFClick()

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Not IsPostBack Then
            If RequestContext.CurrentUser.USER_TYPE = UserType.Customer Then
                Me.txtDiscount.Visible = False
            Else
                Me.txtDiscount.Visible = True
                txtDiscount.Text = Request("discount")
            End If

            Me.chkShowFOB.Visible = True
            chkShowFOB.Checked = Convert.ToBoolean(Convert.ToInt32(Request("showfob")))

            Dim cpc As ConPointCollection = ConPointService.GetConPointsListForIDS()

            Me.ddlConPoint.DataSource = cpc
            Me.ddlConPoint.DataTextField = ConPointColumns.Description.ToString()
            Me.ddlConPoint.DataValueField = ConPointColumns.ConPointID.ToString()
            Me.ddlConPoint.DataBind()

            Me.ddlIDSSort.DataSource = IDSSortService.GetIDSSortsOnIDS()
            Me.ddlIDSSort.DataTextField = IDSSortColumns.Name.ToString()
            Me.ddlIDSSort.DataValueField = IDSSortColumns.IDSSortID.ToString()
            Me.ddlIDSSort.DataBind()

            Me.ddlIDSSort.Items.Insert(0, New ListItem("[All]", "-1"))

            ddlOrderGoods.DataSource = OfficeService.GetOffices(RequestContext.CurrentUser.US_ID_PK)
            ddlOrderGoods.DataTextField = OfficeColumns.OfficeName.ToString()
            ddlOrderGoods.DataValueField = OfficeColumns.OfficeID.ToString()
            ddlOrderGoods.DataBind()

            ddlOrderGoods.Items.Insert(0, New ListItem("[No Order Goods]", "-1"))

            If (RequestContext.CurrentUser.USER_TYPE <> UserType.Customer _
                AndAlso RequestContext.CurrentUser.USER_TYPE <> UserType.Sales) Then
                ddlOrderGoods.Items.Insert(0, New ListItem("[ALL Owner]", "-2"))
            End If

            Try
                ddlOrderGoods.SelectedIndex = ddlOrderGoods.Items.IndexOf(ddlOrderGoods.Items.FindByValue(RequestContext.OwnerID.ToString()))
            Catch ex As Exception
            End Try

            ddlTerritory.DataSource = TerritoryService.GetTerritoriesByCountry(RequestContext.CurrentUser.CountryID)
            ddlTerritory.DataTextField = TerritoryColumns.Description.ToString()
            ddlTerritory.DataValueField = TerritoryColumns.TerritoryID.ToString()
            ddlTerritory.DataBind()

            If (RequestContext.TerritoryID > -1) Then
                Try
                    ddlTerritory.SelectedIndex = ddlTerritory.Items.IndexOf(ddlTerritory.Items.FindByValue(RequestContext.TerritoryID.ToString()))
                Catch ex As Exception
                End Try

                ddlFOBOrHD.SelectedIndex = 1
            Else
                ddlFOBOrHD.SelectedIndex = 0
            End If

            Try
                ddlTarget.SelectedIndex = ddlTarget.Items.IndexOf(ddlTarget.Items.FindByValue(RequestContext.Target.ToString()))
            Catch ex As Exception
            End Try

            Dim cpHTML As String = ""
            Dim loadingPortHTML As String = ""
            Dim i As Integer
            For i = 0 To cpc.Count - 1
                cpHTML += String.Format("cpData[{0}] = ""{1}"";{2}", i, Server.HtmlEncode(cpc(i).ContainerLoading), Environment.NewLine)
                loadingPortHTML += String.Format("cpLoadingPortData[{0}] = ""{1}"";{2}", i, Server.HtmlEncode(cpc(i).LoadingPort), Environment.NewLine)
            Next

            ltrJS.Text = "<script>var cpData = new Array(); var cpLoadingPortData = new Array();"
            ltrJS.Text += cpHTML
            ltrJS.Text += loadingPortHTML
            ltrJS.Text += "</script>"

            Try
                ddlConPoint.SelectedIndex = ddlConPoint.Items.IndexOf(ddlConPoint.Items.FindByValue(RequestContext.ConPointID.ToString()))
            Catch ex As Exception
            End Try


            Dim boType As Int32 = -1
            If Not Request.QueryString("type") Is Nothing Then
                boType = RequestContext.BOType
            End If

            Dim target As Int32 = -1

            txtQty.Text = RequestContext.Quantity.ToString()
            Try
                ddlIDSSort.SelectedIndex = ddlIDSSort.Items.IndexOf(ddlIDSSort.Items.FindByValue(Request.QueryString("sort")))
            Catch ex As Exception

            End Try
        End If

    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
       
    End Sub

    Private Sub btnExportPDF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub ibtnGO_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibtnGO.Click
        Dim discount As Decimal = 0
        Dim chkFOB As Boolean = False
        chkFOB = chkShowFOB.Checked

        If RequestContext.CurrentUser.USER_TYPE <> UserType.Customer _
            Or RequestContext.CurrentUser.IS_ADMIN = True Then
            Try
                discount = Decimal.Parse(txtDiscount.Text)
            Catch ex As Exception
            End Try
        End If

        Dim idsSort As Integer = -1 ' [All]
        Try
            idsSort = Convert.ToInt32(ddlIDSSort.SelectedItem.Value)
        Catch ex As Exception

        End Try

        Dim qty As Integer = 0
        Try
            qty = Convert.ToInt32(txtQty.Text)
        Catch ex As Exception
        End Try

        Dim territoryId As Integer = -1

        If (ddlFOBOrHD.SelectedIndex > 0) Then ' not use FOB
            territoryId = Convert.ToInt32(ddlTerritory.SelectedItem.Value)
        End If

        RaiseEvent OKClick(Int32.Parse(ddlConPoint.SelectedItem.Value), _
                    idsSort, qty, discount, chkFOB, Convert.ToInt32(ddlOrderGoods.SelectedItem.Value), Convert.ToInt32(ddlTarget.SelectedItem.Value), territoryId)
    End Sub

    Private Sub ibtnExportPDF_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibtnExportPDF.Click
        RaiseEvent ExportPDFClick()
    End Sub
End Class
